package com.szh.a0522_shenzhihao.Fragments;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.szh.a0522_shenzhihao.Adapter.BookListViewAdapter;
import com.szh.a0522_shenzhihao.Bean.BookBean;
import com.szh.a0522_shenzhihao.DBHelper.MyOpenHelper;
import com.szh.a0522_shenzhihao.R;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class BookFragment extends Fragment {
    private static final String TAG = "BookFragment";
    ListView listView;
    List<BookBean>bookBeanList;
    private MyOpenHelper myOpenHelper;
    SQLiteDatabase db;
    private BookBean bookBean;
    private BookListViewAdapter adapter;
    Button button;
    public BookFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        myOpenHelper = new MyOpenHelper(getContext());
        db=myOpenHelper.getWritableDatabase();
        bookBeanList=new ArrayList<>();

        Cursor cursor = db.query("book", null, null, null, null, null, "_id desc");
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String author = cursor.getString(cursor.getColumnIndex("author"));
            String count = cursor.getString(cursor.getColumnIndex("count"));
            bookBean = new BookBean(name,author,count);
            bookBeanList.add(bookBean);
        }
        View view=inflater.inflate(R.layout.fragment_book, container, false);
        listView=view.findViewById(R.id.book_lv);
        button=view.findViewById(R.id.btn_insert);
        adapter = new BookListViewAdapter(getContext(), bookBeanList,db,myOpenHelper);
        listView.setAdapter(adapter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
                builder.setTitle("增加书籍");
                builder.setIcon(R.mipmap.ic_launcher);
                View view1=LayoutInflater.from(getContext()).inflate(R.layout.book_dialog,null);
                final EditText bookname=view1.findViewById(R.id.dialog_name);
                final EditText bookauthor=view1.findViewById(R.id.dialog_author);
                final EditText bookcount=view1.findViewById(R.id.dialog_count);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String sql = "insert into book(name,author,count)values(?,?,?)";
                        db.execSQL(sql,new Object[]{bookname.getText().toString(),bookauthor.getText().toString(),bookcount.getText().toString()});
                        adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消",null);
                builder.setView(view1);
                builder.show();
            }
        });
        adapter.notifyDataSetChanged();
        return view;
    }
}
